//
//  HPSimulaTionTableViewCell.swift
//  TLenPoint
//
//  Created by Docker on 2018/3/27.
//  Copyright © 2018年 Docker. All rights reserved.
//

import UIKit

class HPSimulaTionTableViewCell: UITableViewCell {

    //MARK: -- 定义闭包
    var callApplyBack:((UIButton)->())?
    //MARK: -- 定义属性
    var model:HPSimulationListModel?{
    didSet{
        if model == nil{
            return
        }else{
            nameLabel.text = model?.title
            peopleNumber.text = model?.applyUserSum
            putureImageView.sd_setImage(with: URL(string: (model?.imageCodeUri)!), placeholderImage: nil, options: .retryFailed, completed: { (image, error, cacheType, imageURL) in
                
            })
        }
     }
    }
    //MARK: -- 懒加载
    private lazy var putureImageView:UIImageView = UIImageView()
    
    private lazy var nameLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.black
        return label
        }()
    
    private lazy var people:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.gray
        label.text = "参赛人数(人)"
        return label
    }()
    
    private lazy var gameState:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.gray
        label.text = "比赛状态"
        return label
    }()
    
    private lazy var peopleNumber:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = UIColor.red
        label.text = "--"
        return label
    }()
    
    private lazy var gameStateLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.red
        label.text = "--"
        return label
    }()
    
    private lazy var entryfeeLabel:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = UIColor.gray
        label.text = "报名费(积分)"
        return label
    }()
    
    private lazy var endDateLable:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 10)
        label.textColor = UIColor.gray
        label.text = "截止日期"
        return label
    }()
    
    private lazy var entryfeeNumber:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.black
        return label
    }()
    
    private lazy var endDateNumber:UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textColor = UIColor.black
        return label
    }()
    
    private lazy var applyBtn:UIButton = {
        let btn = UIButton(type: UIButtonType.custom)
        btn.setTitle("立即\n报名", for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "Home_SimulationClickRedBtn"), for: UIControlState.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn.titleLabel?.textColor = UIColor.white
        btn.addTarget(self, action: #selector(applyClikAction(btn:)), for: .touchUpInside)
        btn.titleLabel?.numberOfLines = 0
        return btn
    }()
    //MARK: -- 初始化方法
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.creatUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -- 按钮点击事件
    @objc func applyClikAction(btn:UIButton){
        if ((callApplyBack) != nil){
            callApplyBack!(btn)
        }
    }
    //MARK: -- 创建UI
    func creatUI(){
        self.contentView.addSubview(putureImageView)
        putureImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(15)
            make.size.equalTo(CGSize(width: 88, height: 88))
        }
        self.contentView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(putureImageView).offset(3)
            make.left.equalTo(putureImageView.snp.right).offset(14)
        }
        self.contentView.addSubview(people)
        people.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(8)
            make.left.equalTo(nameLabel)
        }
        self.contentView.addSubview(gameState)
        gameState.snp.makeConstraints { (make) in
            make.centerY.equalTo(people)
            make.left.equalTo(putureImageView.snp.right).offset(116)
        }
        self.contentView.addSubview(peopleNumber)
        peopleNumber.snp.makeConstraints { (make) in
            make.left.equalTo(people)
            make.top.equalTo(people.snp.bottom).offset(2)
        }
        self.contentView.addSubview(gameStateLabel)
        gameStateLabel.snp.makeConstraints { (make) in
            make.top.equalTo(peopleNumber)
            make.left.equalTo(putureImageView.snp.right).offset(116)
        }
        self.contentView.addSubview(entryfeeLabel)
        entryfeeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(peopleNumber.snp.bottom).offset(5)
            make.left.equalTo(peopleNumber)
        }
        self.contentView.addSubview(endDateLable)
        endDateLable.snp.makeConstraints { (make) in
            make.left.equalTo(gameStateLabel)
            make.top.equalTo(entryfeeLabel)
        }
        self.contentView.addSubview(entryfeeNumber)
        entryfeeNumber.snp.makeConstraints { (make) in
            make.left.equalTo(entryfeeLabel)
            make.top.equalTo(entryfeeLabel.snp.bottom).offset(5)
        }
        self.contentView.addSubview(endDateNumber)
        endDateNumber.snp.makeConstraints { (make) in
            make.top.equalTo(entryfeeLabel)
            make.left.equalTo(endDateLable)
        }
        self.contentView.addSubview(applyBtn)
        applyBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 50, height: 50))
        }
    }
}
